package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.model.TUser;
import com.dongdongshop.dto.SellerDTO;
import com.dongdongshop.dto.TbUserDTO;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.enums.TokenEnum;
import com.dongdongshop.exception.LoginException;
import com.dongdongshop.service.UserLoginService;
import com.dongdongshop.shiro.LoginToken;
import com.dongdongshop.util.JwtUtil;
import com.dongdongshop.util.UserVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("login")
@RefreshScope
public class LoginController {

    @Autowired
    private UserLoginService userService;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping
    public Result login(@RequestParam String username, @RequestParam String password){

        Subject subject = SecurityUtils.getSubject();
        LoginToken token1 = new LoginToken(username,password, TokenEnum.ADMIN);
        try{
            subject.login(token1);
        }catch (UnknownAccountException ue){
            return Result.response(ResultEnum.UNKNOWN_USER_NAME_PASSWORD);
        }catch (IncorrectCredentialsException ce){
            return Result.response(ResultEnum.UNKNOWN_USER_NAME_PASSWORD);
        }

        UserVO vo = new UserVO();
        TUser user = userService.selectByUsername(username);
        vo.setId(user.getId().toString());
        vo.setName(user.getName());
        String token = jwtUtil.createJwt(vo.getId(), JSONObject.toJSONString(vo), "role");
        return Result.OK().setData(token);
    }

    @PostMapping("sellerLogin")
    public Result sellerLogin(@RequestParam String username, @RequestParam String password){

        Subject subject = SecurityUtils.getSubject();
        LoginToken token1 = new LoginToken(username,password, TokenEnum.SELLER);
        try{
            subject.login(token1);
        }catch (UnknownAccountException ue){
            return Result.response(ResultEnum.UNKNOWN_USER_NAME_PASSWORD);
        }catch (IncorrectCredentialsException ce){
            return Result.response(ResultEnum.UNKNOWN_USER_NAME_PASSWORD);
        }catch (LoginException loginException){
            return Result.response(ResultEnum.ACCOUNT_ERROR);
        }

        SellerDTO dto = (SellerDTO) SecurityUtils.getSubject().getPrincipal();
        UserVO vo = new UserVO();
        vo.setId(dto.getSellerId());
        vo.setName(dto.getName());
        String token = jwtUtil.createJwt(vo.getId(), JSONObject.toJSONString(vo), "role");
        return Result.OK().setData(token);
    }

    @PostMapping("userLogin")
    public Result userLogin(@RequestParam String username, @RequestParam String password){

        Subject subject = SecurityUtils.getSubject();
        LoginToken token1 = new LoginToken(username,password, TokenEnum.USER);
        try{
            subject.login(token1);
        }catch (UnknownAccountException ue){
            return Result.response(ResultEnum.UNKNOWN_USER_NAME_PASSWORD);
        }catch (IncorrectCredentialsException ce){
            return Result.response(ResultEnum.UNKNOWN_USER_NAME_PASSWORD);
        }

        TbUserDTO userDTO = (TbUserDTO) SecurityUtils.getSubject().getPrincipal();
        UserVO vo = new UserVO();
        vo.setId(userDTO.getId().toString());
        vo.setName(userDTO.getUsername());
        String token = jwtUtil.createJwt(vo.getId(), JSONObject.toJSONString(vo), "role");
        return Result.OK().setData(token);
    }

}
